//
//  ClassPhotoCell.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/11/29.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "ClassPhotoCell.h"

@implementation ClassPhotoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    
    //初始化可变数组
    _imageArr = [NSMutableArray array];
    
    
    
    if (self) {
        

        
        
        //时间
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,10,SCREENWIDTH-30,14)];
        _timeLabel.font = [UIFont systemFontOfSize:14];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.textColor = [UIColor colorWithHexString:@"979797"];
        [self.contentView addSubview:_timeLabel];
        
        //昵称
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:13];
        _nameLabel.textColor = [UIColor colorWithHexString:@"979797"];
        _nameLabel.backgroundColor =[UIColor clearColor] ;
        _nameLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_nameLabel];
        
        _line = [[UIView alloc]init];
        _line.backgroundColor = [UIColor colorWithHexString:@"dedede"];
        [self.contentView addSubview:_line];
        
    }
    return self;
}

-(CGRect)setDataForCellWith:(PhotoDetaiModel *)model classNameWith:(NSString *)className withType:(NSString *)type{
    
    _classNameStr = className;
    
    _sourceStr = model.username;
    
    _timeStr = [Utils getProperDataStringWithString:model.jointime];
    
    _typeStr = type;
    
    NSArray * infoFileList = model.infofilelist;
    
    
    NSMutableArray *fileids = [NSMutableArray array];
    
    for (NSDictionary * dic in infoFileList) {
        
        NSString * fileidsStr = [[dic objectForKey:@"fileid"] stringValue];
        
        if (fileidsStr.length != 0) {
            
            [fileids addObject:fileidsStr];
            
        }
    }
    //图片数组
    _images = nil;
    _images = fileids;
    
    for (UIView *subview in _bgView.subviews) {
        [subview removeFromSuperview];
    }
    
    _bgView = nil;
    [_imageArr removeAllObjects];
    
    if (fileids.count != 0) {  //有图片
        
        _bgView = [[UIView alloc] init];
        [self.contentView addSubview:_bgView];
        
        
        NSInteger line = 0;
        if ([fileids count]%3 == 0) {
            line = [fileids count]/3;
        }else
        {
            line = [fileids count]/3 + 1;
        }
        
        _bgView.frame = CGRectMake(0, CGRectGetMaxY(_timeLabel.frame)+10, SCREENWIDTH,(9 + 109) * line - 9);
        
        _bgView.userInteractionEnabled = YES;
        
    }
    
    CGRect rect;
    
    float w = (SCREENWIDTH-30-18)/3;
    
    for (NSInteger index = 0 ;index < [fileids count] ; index ++) {
        
        NSString *iamgename = [fileids objectAtIndex:index];
        rect = CGRectMake(15 + (w + 9) * (index %3),(w + 9) * (index / 3), w, w);
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
        
        imageView.userInteractionEnabled = YES;
        
        NSString * imageString = [NSString stringWithFormat:@"%@%@&s=%d",IMAGEHOSTURL,iamgename,ImageWidht];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"bjxc_emptyPic" ]];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        
        [imageView addGestureRecognizer:tap];
        imageView.tag = index;
        
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        
        [_imageArr addObject:imageView];
        
        [_bgView addSubview:imageView];
        
    }
    
    if (fileids.count != 0) {
        
        rect.origin.y += 34;
        
        [self.nameLabel setFrame:CGRectMake(15, rect.origin.y + rect.size.height+12, SCREENWIDTH-30, 13)];
        [self.line setFrame:CGRectMake(15, self.nameLabel.frame.origin.y+self.nameLabel.frame.size.height+12, SCREENWIDTH-15, 0.5)];
    }
    
    if (model.jointime.length>0) {
        
    self.timeLabel.text = [NSString stringWithFormat:@"-%@-",_timeStr];
    }

    self.nameLabel.text = [NSString stringWithFormat:@"-来自%@",model.username];
    
    return rect;
    
}

-(void)tapAction:(UIGestureRecognizer *)gesture{
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = _bgView;
    browser.imageCount = _images.count;
    browser.titleStr = _classNameStr;
    browser.sourceStr = _sourceStr;
    browser.timeStr = _timeStr;
    browser.typeStr = _typeStr;
    browser.currentImageIndex = (int)gesture.view.tag;
    browser.delegate = self;
    [browser show];
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    UIImageView *imageView = _imageArr[index];
    return imageView.image;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = _images[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
        logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    
    NSURL *url = [NSURL URLWithString:logoStr];
    return url;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
